package com.lanf.crm.web.interceptor;

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @program: crm
 * @description: 用户登录拦截器
 * @author: 蓝建平
 * @create: 2020-10-04 09:03
 **/
@Component
public class LoginInterceptor  implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //如果用户未登录，且访问的是/admin下的地址，则
        Object user = request.getSession().getAttribute("user");
        String reqUri = request.getRequestURI();
        if(reqUri.startsWith("/admin/")&&user==null)
        {
            response.sendRedirect("/login");
            return false;
        }
        return true;
    }
}
